  function fighand=plot_decompositions(x,cmat,obsnames,shonames,sample,tit,yoffset,flag_color);
% =========================================================================
% function plot_decompositions(obs,cmat,obsnames,shonames,title,offset); 
% Given an observable series OBS [T 1]
% Plot is together with the series obtained as each shock fed, one at a
% time in the matrix CMAT [T nx] where nx is number of shocks. 
% OBSNAMES (string or cell)     Name of the series 
% SHONAMES [nx 1] (cell)        Name of shocks 
% SAMPLE                        Sample vector 
% OFFSET   scalar               Determine widening of Y axis relative to Max/Min values 
%
% Alejandro Justiniano January 21st 2010 
% =========================================================================
if nargin < 8 || isempty(flag_color);
    flag_color=1;
end
if nargin < 7 || isempty(yoffset); 
    yoffset=0; 
end 
if nargin < 6 || isempty(tit); 
    flag_title=0; 
else 
    flag_title=1; 
end 
t=length(x(:)); 
if size(cmat,1)~=t;error('Input and decomposition matrix do not match');end 
if length(sample)~=t;error('Input and sample vector do not match');end 
nx=size(cmat,2); 
if length(shonames)~=nx;error('Columns decomposition matrix and shonames do not match');end 
widthvec=[2.5 2]; 
ymin=min(min([x cmat]));
ymax=max(max([x cmat]));
ymin=ymin*(1-sign(ymin)*yoffset); 
ymax=ymax*(1+sign(ymax)*yoffset); 
fighand=figure;

NRows=3; 
NCols=2; 
ISubplots=0; 
IGraphs  =0; 
ICounter =0; 
graphsVec =zeros( ceil(nx/(NRows*NCols)), 1); 
NSubplots=nx; 

for ii=1:NSubplots   
    if ISubplots==0
        IGraphs=IGraphs+1;
        graphsVec(IGraphs)=figure;
    end
    ISubplots=ISubplots+1;
    ICounter =ICounter+1;    
    subplot(NRows,NCols,ICounter); 
    hand1=plot(sample,cmat(:,ii),'LineWidth',widthvec(1));hold on;
    hand2=plot(sample,x,'--','LineWidth',widthvec(2));
    % Color
    if flag_color==1
        set(hand1,'Color','b')
        set(hand2,'Color','r','MarkerSize',2);  hold off;
    else
        veclight=[0.55 0.55 0.55];
        set(hand1,'Color',[0.0 0.0 0.0],'LineWidth',widthvec(1));
        set(hand2,'Color',veclight,'LineStyle','--','MarkerSize',1,'LineWidth',widthvec(2));
    end
    ylim([ymin ymax]);         
    xlim([sample(1) sample(end)]);            
    if t <= 8
        set(gca,'Xtick',sample(1):1:sample(end))
    elseif t > 8 && t <= 16
        set(gca,'Xtick',sample(1):2:sample(end))
    else
        set(gca,'Xtick',sample(1):4:sample(end))
    end
    title(shonames{ii},'FontSize',11,'FontName','Helvetica'); 
    
    if ISubplots==NRows*NCols || ii==NSubplots
        ISubplots=0;
        suptitle('True (solid) vs. Filtered (dashed) States');
        set(graphsVec(IGraphs),'PaperPosition',[0.75 0.5 7.5 10])
    end
    
    
end 
set(fighand,'PaperPosition',[0.75 0.5 7.5 10])
if flag_title==0 
    hand=suptitle([obsnames,' decomposition across shocks']); 
else 
    hand=suptitle(tit); 
end 
set(hand,'FontSize',14,'FontName','Helvetica');
